import 'package:flutter/material.dart';
import 'package:hive/hive.dart';
import 'package:provider/provider.dart';
import 'package:tests/ThemePage.dart';
import 'package:tests/counter.dart';
import 'package:tests/themePro.dart';
import 'package:path_provider/path_provider.dart' as path_provider;

void main() async {
  // 确保初始化
  WidgetsFlutterBinding.ensureInitialized();

  final appPath = await path_provider.getApplicationDocumentsDirectory();
  Hive.init(appPath.path);
  ThemePro themePro = ThemePro();
  await themePro.init();

  runApp(
    ChangeNotifierProvider.value(value: themePro,child: MyApp(),)
  );
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Consumer<ThemePro>(builder: (ctx, obj, child) {
      return MaterialApp(
        title: 'Flutter Demo',
        theme: obj.themeData,
        home: MyHomePage(title: 'Flutter Demo Home Page'),
      );
    });
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);


  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;
  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {

    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: ChangeNotifierProvider(
        create: (_) => Counter(0),
        child: Body(),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}

class Body extends StatefulWidget {
  @override
  _BodyState createState() => _BodyState();
}

class _BodyState extends State<Body> {
  @override
  Widget build(BuildContext context) {
    return Center(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          Text(
            'You have pushed the button this many times:',
          ),
          Consumer2<Counter, ThemePro>(
            builder: (ctx, counter, themes,child) {
              return Text(
                counter.counter.toString(),
                style: TextStyle(
                  color: themes.customTheme.titleColor,
                  fontSize: 29
                ),
              );
            },
          ),
          MaterialButton(
            onPressed: dart,
            child: Text('设置主题'),
          ),
        ],
      ),
    );
  }


  void dart() {
    Navigator.push(context, MaterialPageRoute(builder: (ctx) {
      return ThemePage();
    }));
  }

}
